package com.ks.pc.controller.asyn;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ks.dto.BrandDto;
import com.ks.dto.ClassifyDto;
import com.ks.dto.PaginationDto;
import com.ks.dto.ProductDto;
import com.ks.dto.ResultDto;
import com.ks.pc.service.CommonService;
import com.ks.pc.service.SysProductService;

@Controller
@RequestMapping("/sys")
public class SysProductControllerAsyn {

	@Autowired
	private SysProductService sysProductService;
	@Autowired
	private CommonService commonService;


	@ResponseBody
	@RequestMapping(value = "/findClassifyById", method = RequestMethod.POST)
	public ClassifyDto findClassifyById(String classifyId) {
		ClassifyDto dtos = sysProductService.findClassifyById(classifyId);
		return dtos;
	}

	@RequestMapping(value = "/updateDtoById", method = RequestMethod.POST)
	public String updateDtoById(boolean classObj, Integer classifyId, Integer brandId, String name, Integer hasPublic,
			Integer showIndex) {
		boolean success = false;
		if (null != classifyId) {
			ClassifyDto dto = new ClassifyDto();
			dto.setId(classifyId);
			dto.setName(name);
			dto.setHasPublic(hasPublic);
			dto.setShowIndex(showIndex);
			success = sysProductService.updateDtoById("ProductMapper.updateClassifyById", dto);
		}
		if (null != brandId) {
			BrandDto dto = new BrandDto();
			dto.setId(brandId);
			dto.setName(name);
			dto.setHasPublic(hasPublic);
			dto.setShowIndex(showIndex);
			success = sysProductService.updateDtoById("ProductMapper.updateBrandById", dto);
		}
		if (success) {
			return "biz/sysProduct";
		}
		return null;
	}

	@ResponseBody
	@RequestMapping(value = "/findBrandById", method = RequestMethod.POST)
	public BrandDto findBrandById(String brandId) {
		BrandDto dtos = sysProductService.findBrandById(brandId);
		return dtos;
	}

	@RequestMapping(value = "/saveDto", method = RequestMethod.POST)
	public String saveDto(Integer classifyId, String name, Integer hasPublic, Integer showIndex) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("classifyId", classifyId);
		param.put("name", name);
		param.put("hasPublic", hasPublic);
		param.put("showIndex", showIndex);
		String key = sysProductService.saveDto(param);
		return "biz/sysProduct";
	}

	@ResponseBody
	@RequestMapping(value = "/findProductGrid", method = RequestMethod.POST)
	public PaginationDto<ProductDto> findProductGrid(String brandId, String searchTxt) {
		PaginationDto<ProductDto> dto = new PaginationDto<ProductDto>(1, 20);
		Map<String, Object> param = new HashMap<String, Object>();
		if (",".contains(brandId)) {
			param.put("brandId", brandId.split(",")[0]);
		} else {
			param.put("brandId", brandId);
		}
		param.put("searchTxt", searchTxt);
		dto = sysProductService.findProductGrid(dto, param);
		return dto;
	}

	@ResponseBody
	@RequestMapping(value = "/deleteProductByIds", method = RequestMethod.POST)
	public ResultDto<String> deleteProductByIds(String idsArr) {
		ResultDto<String> result = new ResultDto<String>();
		boolean rs = sysProductService.deleteProductByIds(idsArr);
		result.setType(rs);
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/deleteBrandByIds", method = RequestMethod.POST)
	public ResultDto<String> deleteBrandByIds(String idsArr) {
		ResultDto<String> result = new ResultDto<String>();
		boolean rs = sysProductService.deleteBrandByIds(idsArr);
		result.setType(rs);
		return result;
	}

}
